package com.strive.config;

import com.strive.exception.CustomException;
import com.strive.filter.RequestIdFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/3/2 9:57
 * @Version 1.0
 */
@Configuration
@EnableResourceServer
public class ResourceServiceConfig extends ResourceServerConfigurerAdapter{

    @Override
    public void configure(HttpSecurity http) throws Exception {
        String[] urlList = SpringSecurityConfig.getUrlList();
        http.authorizeRequests().antMatchers(urlList)
                .permitAll()
                .anyRequest().authenticated()
                .and().addFilterBefore(new RequestIdFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint(new CustomException());
    }
}
